﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using T.ApprovalRecord;
using T.Personnel.Repository.Interface;

namespace T.Personnel.Repository.Impl
{
    public class BaseApprovalRepository : BaseRepository<ApprovalRecordd>, IApprovalRepository
    {
        private readonly EFDbcontext db;
        private readonly IManageRepository manageRepository;
        private readonly IRegisterRepository registerRepository;
        public BaseApprovalRepository(EFDbcontext eFDbcontext, EFDbcontext db, IManageRepository manageRepository, IRegisterRepository registerRepository) : base(eFDbcontext)
        {
            this.db = db;
            this.manageRepository = manageRepository;
            this.registerRepository = registerRepository;
        }

        public async Task<int> AddEntryApprovalAsync(ApprovalRecordd approvalRecordd)
        {
            using (var transaction = await db.Database.BeginTransactionAsync())
            {
                try
                {
                    var allor = await manageRepository.GetAsync(x => x.Id == approvalRecordd.PersonnelId);

                    var alltoer = await registerRepository.GetAsync(x=>x.Id==allor.StaffId);

                    if (approvalRecordd.ApprovalResult == "同意并转批")
                    {
                        allor.Subgrantor = approvalRecordd.Subgrantor;
                        allor.UpdatedBy = approvalRecordd.UpdatedBy;
                        allor.UpdatedDate = approvalRecordd.UpdatedDate;
                        await manageRepository.UpdAsync(allor);
                    }
                    else if (approvalRecordd.ApprovalResult == "同意并终止审批")
                    {
                        alltoer.State = "试用期";
                        alltoer.EmployeeType = "试用工";
                        await registerRepository.UpdAsync(alltoer);
                        allor.AppStatus = "已同意";
                        allor.Approver = approvalRecordd.Approver;
                        allor.UpdatedBy = approvalRecordd.UpdatedBy;
                        allor.UpdatedDate = approvalRecordd.UpdatedDate;
                        await manageRepository.UpdAsync(allor);
                    }
                    else if (approvalRecordd.ApprovalResult == "拒绝")
                    {
                        allor.AppStatus = "已拒绝";
                        allor.Approver = approvalRecordd.Approver;
                        allor.UpdatedBy = approvalRecordd.UpdatedBy;
                        allor.UpdatedDate = approvalRecordd.UpdatedDate;
                        await manageRepository.UpdAsync(allor);
                    }

                    var result = await base.AddsAsync(approvalRecordd);

                    // 提交事务
                    await transaction.CommitAsync();  // 异步提交事务

                    return result;
                }
                catch (Exception ex)
                {
                    // 事务回滚
                    await transaction.RollbackAsync();  // 异步回滚事务

                    // 可以在这里记录日志，方便调试
                    // logService.LogError(ex);

                    // 抛出异常到上层
                    throw new ApplicationException("Error while adding entry.", ex);
                }
            }
        }

        public async Task<int> AddFullApprovalAsync(ApprovalRecordd approvalRecordd)
        {
            using (var transaction = await db.Database.BeginTransactionAsync())
            {
                try
                {
                    var allor = await manageRepository.GetAsync(x => x.Id == approvalRecordd.PersonnelId);

                    var alltoer = await registerRepository.GetAsync(x => x.Id == allor.StaffId);

                    if (approvalRecordd.ApprovalResult == "同意并转批")
                    {
                        allor.Subgrantor = approvalRecordd.Subgrantor;
                        allor.UpdatedBy = approvalRecordd.UpdatedBy;
                        allor.UpdatedDate = approvalRecordd.UpdatedDate;
                        await manageRepository.UpdAsync(allor);
                    }
                    else if (approvalRecordd.ApprovalResult == "同意并终止审批")
                    {
                        alltoer.State = "已转正";
                        alltoer.EmployeeType = "正式员工";
                        await registerRepository.UpdAsync(alltoer);
                        allor.AppStatus = "已同意";
                        allor.Approver = approvalRecordd.Approver;
                        allor.UpdatedBy = approvalRecordd.UpdatedBy;
                        allor.UpdatedDate = approvalRecordd.UpdatedDate;
                        await manageRepository.UpdAsync(allor);
                    }
                    else if (approvalRecordd.ApprovalResult == "拒绝")
                    {
                        allor.AppStatus = "已拒绝";
                        allor.Approver = approvalRecordd.Approver;
                        allor.UpdatedBy = approvalRecordd.UpdatedBy;
                        allor.UpdatedDate = approvalRecordd.UpdatedDate;
                        await manageRepository.UpdAsync(allor);
                    }

                    var result = await base.AddsAsync(approvalRecordd);

                    // 提交事务
                    await transaction.CommitAsync();  // 异步提交事务

                    return result;
                }
                catch (Exception ex)
                {
                    // 事务回滚
                    await transaction.RollbackAsync();  // 异步回滚事务

                    // 可以在这里记录日志，方便调试
                    // logService.LogError(ex);

                    // 抛出异常到上层
                    throw new ApplicationException("Error while adding entry.", ex);
                }
            }
        }

        public async Task<int> AddResignationApprovalAsync(ApprovalRecordd approvalRecordd)
        {
            using (var transaction = await db.Database.BeginTransactionAsync())
            {
                try
                {
                    var allor = await manageRepository.GetAsync(x => x.Id == approvalRecordd.PersonnelId);

                    var alltoer = await registerRepository.GetAsync(x => x.Id == allor.StaffId);

                    if (approvalRecordd.ApprovalResult == "同意并转批")
                    {
                        allor.Subgrantor = approvalRecordd.Subgrantor;
                        allor.UpdatedBy = approvalRecordd.UpdatedBy;
                        allor.UpdatedDate = approvalRecordd.UpdatedDate;
                        await manageRepository.UpdAsync(allor);
                    }
                    else if (approvalRecordd.ApprovalResult == "同意并终止审批")
                    {
                        alltoer.State = "已离职";
                        alltoer.EmployeeType = "已离职";
                        await registerRepository.UpdAsync(alltoer);
                        allor.AppStatus = "已同意";
                        allor.Approver = approvalRecordd.Approver;
                        allor.UpdatedBy = approvalRecordd.UpdatedBy;
                        allor.UpdatedDate = approvalRecordd.UpdatedDate;
                        await manageRepository.UpdAsync(allor);
                    }
                    else if (approvalRecordd.ApprovalResult == "拒绝")
                    {
                        allor.AppStatus = "已拒绝";
                        allor.Approver = approvalRecordd.Approver;
                        allor.UpdatedBy = approvalRecordd.UpdatedBy;
                        allor.UpdatedDate = approvalRecordd.UpdatedDate;
                        await manageRepository.UpdAsync(allor);
                    }

                    var result = await base.AddsAsync(approvalRecordd);

                    // 提交事务
                    await transaction.CommitAsync();  // 异步提交事务

                    return result;
                }
                catch (Exception ex)
                {
                    // 事务回滚
                    await transaction.RollbackAsync();  // 异步回滚事务

                    // 可以在这里记录日志，方便调试
                    // logService.LogError(ex);

                    // 抛出异常到上层
                    throw new ApplicationException("Error while adding entry.", ex);
                }
            }
        }

        public async Task<List<ApprovalRecordd>> GetApprovalAsync(int id)
        {
            var gather = await base.GetListAsync(x => x.PersonnelId == id);
            return gather.ToList();
        }
    }
}
